
package com.example.mam.service.impl;

import com.example.mam.entity.Investor;
import com.example.mam.entity.Master;
import com.example.mam.entity.Mt5Accounts;
import com.example.mam.entity.Mt5Users;
import com.example.mam.exception.BizException;
import com.example.mam.exception.BizExceptionEnum;
import com.example.mam.mapper.InvestorMapper;
import com.example.mam.mapper.MasterMapper;
import com.example.mam.mapper.Mt5AccountsMapper;
import com.example.mam.mapper.Mt5UsersMapper;
import com.example.mam.service.InvestorService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

@Service
public class InvestorServiceImpl implements InvestorService {

    @Resource
    private InvestorMapper investorMapper;
    @Resource
    private Mt5UsersMapper mt5UsersMapper;
    @Resource
    private MasterMapper masterMapper;


    @Override
    public Integer insertInvestor(Investor investor) {
        //查询投资者账户余额
        Mt5Users mt5Users = mt5UsersMapper.selectByPrimaryKey(investor.getLogin());

        //查询主账号
        Master master = masterMapper.selectMasterByLogin(investor.getMasterLogin());

        if (ObjectUtils.isEmpty(master.getMfLogin()) || ObjectUtils.isEmpty(master.getPfLogin())) {
            //主账号未绑定PF账号或者BF账号          异常码 1124
            throw new BizException(BizExceptionEnum.NGROUPMASTERINVALID_ERROR);
        }
        if (mt5Users.getBalance() < master.getInvestorInitialFunds()) {
            //投资者余额度 小于 主账号设置的 最小投资金额        异常码 1140
            throw new BizException(BizExceptionEnum.NLESSINITIALINVESTMENTS_ERROR);
        }

        investor.setInitialFunds(mt5Users.getBalance());
        investor.setName(mt5Users.getName());
        investor.setCreateTime(new java.util.Date(System.currentTimeMillis()));
        Integer restCode = investorMapper.insert(investor);
        return restCode;
    }
}
